public class Test4 {
    public static void main(String[] args) {
        int[][] nums = {{1,3,5,9},{8,1,3,4},{5,0,6,1},{8,8,4,0}};
        minPathSum(nums);
    }
    public static int minPathSum (int[][] matrix) {
        // write code here
        int m = matrix.length;
        int n = matrix[0].length;
        int INF = 0x3f3f3f3f;
        int[][] dp = new int[m + 1][n + 1];
        for(int i = 1; i <= m; i++) dp[i][0] = INF;
        for(int j = 1; j <= n; j++) dp[0][j] = INF;
        dp[1][0] = 0;
        for(int i = 1; i <= m; i++) {
            for(int j = 1; j <= n; j++) {
                dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + matrix[i - 1][j - 1];
            }
        }

        return dp[m][n];
    }
}
